package com.aopcinemas.visitor.ui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.common.ui.ScheduleTable;
import com.aopcinemas.visitor.VisitorAgent;
import com.aopcinemas.visitor.behaviour.CampaignRegistrationBehavior;
import com.aopcinemas.visitor.model.Campaign;
import com.aopcinemas.visitor.model.Campaign.Type;

public class CampaignCreationDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private ScheduleTable table;
    private VisitorAgent agent;

    public CampaignCreationDialog(JFrame owner, VisitorAgent agent) {
        super(owner, true);
        this.agent = agent;

        table = new ScheduleTable();
        JScrollPane tableScrollPane = new JScrollPane(table);
        getContentPane().add(tableScrollPane);

        JPanel panel = new JPanel();
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(3, 3, 3, 3);
        panel.setLayout(layout);

        JLabel label = new JLabel("Number of participants: ");
        constraints.anchor = GridBagConstraints.EAST;
        constraints.gridwidth = 1;
        layout.setConstraints(label, constraints);
        panel.add(label);

        final JTextField numberOfContributors = new JTextField(10);
        constraints.anchor = GridBagConstraints.WEST;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        layout.setConstraints(numberOfContributors, constraints);
        panel.add(numberOfContributors);

        label = new JLabel("Group visit type: ");
        constraints.anchor = GridBagConstraints.EAST;
        constraints.gridwidth = 1;
        layout.setConstraints(label, constraints);
        panel.add(label);

        final JComboBox campaingType = new JComboBox(Type.values());
        campaingType.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent evt) {
                if (campaingType.getSelectedItem().equals(Type.DOUBLE_ROMANTIC)) {
                    numberOfContributors.setText("2");
                    numberOfContributors.setEnabled(false);
                } else if (campaingType.getSelectedItem().equals(Type.LONE_WOLF)) {
                    numberOfContributors.setText("1");
                    numberOfContributors.setEnabled(false);
                } else {
                    numberOfContributors.setEnabled(true);
                }

            }
        });
        constraints.anchor = GridBagConstraints.WEST;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        layout.setConstraints(campaingType, constraints);
        panel.add(campaingType);

        JButton register = new JButton("Register campaing");
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {

                Campaign campaign = new Campaign();
                Schedule schedule = table.getSelectedSchedule();
                if (schedule == null) {
                    JOptionPane.showMessageDialog(CampaignCreationDialog.this, "Please select schedule");
                    return;
                }
                campaign.setSchedule(schedule);
                campaign.setNumberOfParticipant(Integer.parseInt(numberOfContributors.getText()));
                campaign.getParticipants().add(CampaignCreationDialog.this.agent.getAID());
                campaign.setType((Type) campaingType.getSelectedItem());

                CampaignCreationDialog.this.agent.setCampaign(campaign);
                CampaignCreationDialog.this.agent.addBehaviour(new CampaignRegistrationBehavior(CampaignCreationDialog.this.agent));
            }
        });
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.gridwidth = 2;
        layout.setConstraints(register, constraints);
        panel.add(register);

        /*
         * JButton unregister = new JButton("Unregister");
         * unregister.addActionListener(new ActionListener() {
         * 
         * @Override public void actionPerformed(ActionEvent arg0) {
         * CampaignCreationDialog.this.agent.addBehaviour(new
         * CampaignRegistrationBehavior(CampaignCreationDialog.this.agent,
         * AbstractRegistrationBehavior.UNREGISTER)); } });
         * panel.add(unregister);
         */
        getContentPane().add(panel, BorderLayout.SOUTH);

        setSize(400, 300);
    }

    public void setSchedules(List<Schedule> schedules) {
        table.setSchedules(schedules);
        table.revalidate();
        table.repaint();
    }

}
